<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片面板 -->
      <el-card class="box-card">
        <!-- tabs -->
        <el-tabs v-model="activeName">
          <el-tab-pane class="tab-pane" label="组织架构" name="first">
            <!-- layout 布局 -->
            <el-row type="flex" class="department-header">
              <el-col :span="20">
                <!--左侧部分： 公司 -->
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <!-- 右侧部分 -->
                <el-row type="flex">
                  <el-col> 负责人 </el-col>
                  <el-col>
                    <!-- 下拉菜单 dropdown -->
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作
                        <i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDepts('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 树形组件 -->
            <!--
              data： 渲染的数据
              props：配置项
              default-expand-all 默认展开所有项
             -->
            <el-tree
              class="departments-tree"
              :data="treeData"
              :props="defaultProps"
              default-expand-all
            >
              <!-- 插槽 -->
              <template v-slot="{ data }">
                <!-- 复用上面的 layout 布局 -->
                <el-row type="flex" style="width: 100%">
                  <el-col :span="20">
                    <!--左侧部分：部门 -->
                    <span>{{ data.name }} </span>
                  </el-col>
                  <el-col :span="4">
                    <!-- 右侧部分 -->
                    <el-row type="flex">
                      <el-col> {{ data.manager }} </el-col>
                      <el-col>
                        <!-- 下拉菜单 dropdown -->
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作
                            <i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addDepts(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDepts(data.id)"
                            >编辑部门</el-dropdown-item>
                            <!-- 当前部门如果存在子部门，不允许出现删除按钮 -->
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDepts(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 弹出层 -->
        <el-dialog
          :title="isEdit ? '编辑组织架构':'添加组织架构'"
          :visible.sync="isDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <dept-dialog
            v-if="isDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origan-list="origanList"
            @addDeptEvent="addDeptFn"
            @editDeptEvent="editDeptFn"
            @closeEvent="closeDialog"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartmentsAPI } from '@/api/departments'
import { listToTreeData } from '@/utils/index'
import deptDialog from './deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first',
      treeData: [],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      isDialog: false, // 控制对话框显示/隐藏
      parentId: '',
      isEdit: false,
      origanList: [] // 精简后的数据接收

    }
  },
  created() {
    // 调用 查询企业的部门列表
    this.getDepartments()
  },
  methods: {
    // 删除部门
    async delDepts(id) {
      const confirmRes = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('您已取消删除')
      await delDepartmentsAPI(id)
      this.getDepartments()
      this.$message.success('您已成功删除此部门')
    },
    // 编辑部门
    editDepts(id) {
      /* 显示对话框 */
      this.isDialog = true
      this.parentId = id
      this.isEdit = true
    },
    /* 点击显示弹框 */
    addDepts(id) {
      /* 显示对话框 */
      this.parentId = id
      this.isDialog = true
      this.isEdit = false
    },
    // 编辑部门，关闭弹框，刷新页面
    editDeptFn() {
      this.isDialog = false
      this.getDepartments()
    },
    // 添加部门，关闭弹框，刷新页面
    addDeptFn() {
      this.isDialog = false
      this.getDepartments()
    },
    // 查询企业的部门列表
    async getDepartments() {
      const res = await getDepartments()
      // 精简数据
      this.origanList = res.data.depts.map(item => ({
        name: item.name,
        code: item.code,
        id: item.id,
        pid: item.pid
      }))
      // console.log(this.origanList)
      this.treeData = listToTreeData(res.data.depts)
    },
    // 点击取消关闭弹框
    closeDialog() {
      this.isDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.departments-tree {
  margin: 12px 0px;
}
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
